package com.rockxin.cat.activity;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.ToastUtils;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by cat on 18-3-2.
 */

public class SetupPaymentPasswdActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_payment_passwd)
    EditText etPaymentPasswd;
    @BindView(R.id.et_set_payment_passwd)
    EditText etSetPaymentPasswd;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private String token;

    @Override
    protected int setLayout() {
        return R.layout.activity_setup_payment_passwd;
    }

    @Override
    public void initData() {
        initView();
        setListener();
    }

    public void initView() {
        title.setText("设置支付密码");
        etPaymentPasswd.setSingleLine();
        etSetPaymentPasswd.setSingleLine();
        etPaymentPasswd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        etSetPaymentPasswd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        Intent getIntent = getIntent();
        token = getIntent.getStringExtra("token");
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
    }

    private void submit() {
        final String pass = etPaymentPasswd.getText().toString();
        final String pass1 = etSetPaymentPasswd.getText().toString();
        if (pass.length() < 6) {
            ToastUtils.showShortToastSafe("密码不能小于6位");
            return;
        } else if (pass1.length() < 6) {
            ToastUtils.showShortToastSafe("重复密码不能小于6位");
            return;
        } else if (!pass.equals(pass1)) {
            ToastUtils.showShortToastSafe("密码不一致");
            return;
        }
        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.addpaypwd(token, pass, pass, new Subscriber<HttpResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                Logger.json(new Gson().toJson(httpResult));
                if (httpResult.getStatus() == 1) {
                    etPaymentPasswd.setText("");
                    etSetPaymentPasswd.setText("");
                    finish();
                }
                ToastUtils.showShortToastSafe(httpResult.getInfo());
            }
        });
    }

}
